import { axiosMethods } from '@/utils/request'
import { PAGESIZE, TABLEHEIGHT } from '@/config/AppConstConfig'
import { isArray } from '@/utils/validate'
import { uuid } from '@/utils/util'
import TreeColumn from '../components/TreeColumn.vue'
export default {
  name: 'NbTable',
  props: {
    // 是否显示复选框
    checkbox: {
      type: Boolean,
      default: false
    },
    // 数据源
    data: {
      // type: Any,
      default() {
        return {
          total: 0,
          rows: []
        }
      }
    },
    // 绑定的列名称
    columns: {
      type: Array,
      default() {
        return [
          {
            // 绑定的字段
            field: '',
            // 字段显示文字
            label: '',
            // 是否显示气泡
            tip: true,
            width: 'auto',
            // 是否是自定义栏目
            slotName: null,
            // 用来格式化内容
            formatter: null,
            // eslint-disable-next-line no-irregular-whitespace
            // 列是否固定在左侧或者右侧，true 表示固定在左侧 |　rue, left, right
            fixed: null
          }
        ]
      },
      required: true
    },
    // 请求方式 GET POST
    method: {
      type: String,
      default: ''
    },
    // 加载远程数据接口地址
    url: {
      type: String,
      default: ''
    },
    // 加载数据时候请求参数
    queryParams: {
      type: Object,
      default() {
        return {}
      }
    },
    // 是否分页
    pagination: {
      type: Boolean,
      default: false
    },
    // 当前页
    pageNo: {
      type: Number,
      default: 1
    },
    // 每页显示大小
    pageSize: {
      type: Number,
      default: PAGESIZE
    },
    height: String | Number
  },
  data() {
    //
    return {
      _height: this.height || 'auto',
      Dtdata: this.data,
      DtqueryParams: this.queryParams || {},
      DtpageNo: this.pageNo || 1,
      // 加载等待效果
      loading: false
    }
  },
  components: {
    'tree-column': TreeColumn
  },
  mounted() {
    this.fetchList()
  },
  methods: {
    uuid,
    isArray(data) {
      return Object.prototype.toString.call(data) === '[object Array]'
    },
    fetchList() {
      //
      const url = this.url
      if (url && url != '') {
        const params = Object.assign({}, this.DtqueryParams, { pageNo: this.DtpageNo, pageSize: this.pageSize })
        const method = this.method.toLowerCase() === 'get' ? 'get' : 'post'
        this.loading = true
        axiosMethods[method](url, params).then(data => {
          this.loading = false
          this.Dtdata = data
        })
      }
    },
    reload(params) {
      if (params) {
        Object.assign(this.DtqueryParams, params)
      }
      this.fetchList()
    },
    load(params) {
      if (params) {
        Object.assign(this.DtqueryParams, params)
      }
      this.DtpageNo = 1
      this.fetchList()
    },
    pageChange(val) {
      this.DtpageNo = val
      this.$emit('page-change', { pageNo: val, pageSize: this.pageSize })
    },
    getSelections() {
      return this.$refs.multipleTable.selection
    }
  },
  computed: {
    // 返回标准的数据格式{data:0,rows:[]}
    Data() {
      if (this.isArray(this.Dtdata)) {
        return {
          total: this.Dtdata.length,
          rows: this.Dtdata
        }
      } else {
        return this.Dtdata
      }
    },
    // 根据pagSize计算高度
    heightSize() {
      if (this.height != undefined) {
        return this._height
      } else if (this.pagination) {
        return (this.pageSize + 1) * 40
      }

      return 'auto'
    }
  },
  watch: {
    height(val) {
      this._height = val
    },
    data(val) {
      this.Dtdata = val
    },
    pageNo(val) {
      //
      this.DtpageNo = val
    },
  }
}
